//
//  XJStatusListViewModel.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/24.
//  Copyright © 2016年 452608533. All rights reserved.
//

import UIKit

class XJStatusListViewModel: NSObject {
    
    //全局的模型数组
    
    lazy var statusList: [XJStatusViewModel] = [XJStatusViewModel]()
}

extension XJStatusListViewModel{
    //加载主页面的数据
     
    func loadHomeData(isPullUp:Bool,isFinish:(isSuc:Bool)->()){
        
        var maxId: Int64 = 0
        var sinceId: Int64 = 0
        
        if isPullUp{
            maxId = statusList.last?.status?.id ?? 0
            
            if maxId > 0{
                maxId -= 1
            }
        }else{
            sinceId = statusList.first?.status?.id ?? 0
        }
        
        XJNetworkTools.sharedTools.homeLoadData(sinceId, maxId: maxId, success: { (response) -> () in
            //判断是否为nil 且是否能够转成数组
//            print(response)
            guard let res = response?["statuses"] as?[[String: AnyObject]] else{
                isFinish(isSuc: false)
                return
            }
            //定义一个可变数组
            var tempArray: [XJStatusViewModel] = [XJStatusViewModel]()
            
            for dic in res{
                let status = XJStatus(dict: dic)
                //创建一个viewModel
                let statusViewModel = XJStatusViewModel(status: status)
                tempArray.append(statusViewModel)
            }
            if isPullUp {
                self.statusList = self.statusList + tempArray
            }else{
                self.statusList = tempArray + self.statusList
            }
//            self.statusList = tempArray
            isFinish(isSuc: true)
            }) { (error) -> () in
                isFinish(isSuc: false)
        }
    }
}
